//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013/7/25
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using Forest.Lion.Model;

    public class CompanyRepository : BaseRepository, ICompanyRepository {
        public CompanyRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Company company) {
            this.GetDbSet<Company>().Add(company);
            this.UnitOfWork.SaveChanges();
        }

        public Company GetCompany(Guid companyId) {
            return this.GetDbSet<Company>()
                .Where(c => c.Id == companyId)
                .Single();
        }

        public Company GetCompanyByUserName(string userName) {
            return this.GetDbSet<Company>()
                .Where(c => c.UserName == userName)
                .Single();
        }

        public void Update(Company updatedCompany) {
            this.GetDbSet<Company>().Attach(updatedCompany);
            this.SetEntityState(updatedCompany, updatedCompany.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid companyId) {
            Company companyToDelete = this.GetCompany(companyId);
            this.GetDbSet<Company>().Remove(companyToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Company> Find() {
            return this.GetDbSet<Company>().Take(100)
                .ToList();
        }

        public IEnumerable<Company> GetAll() {
            return this.GetDbSet<Company>().Take(100)
                .ToList();
        }
    }
}
